<!DOCTYPE html>
<!-- See the latest version at https://github.com/yeasy/dockerfileGen -->
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="Dockerfile Generator, see http://github.com/yeasy/dockerfileGen">
    <meta name="author" content="yeasy@github.com">

    <title>Dockerfile Generator</title>

    <!-- Bootstrap, duplicated incase offline -->
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

    <!-- Optional theme -->
    <link rel="stylesheet" href="css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">

    <!-- Latest compiled and minified JavaScript -->
    <script src="js/bootstrap.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script src="js/jquery-1.10.2.min.js"></script>
    <script src="http://www.sanwebe.com/wp-content/themes/sanwebe/js/jquery-1.10.2.min.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

    <script type="text/javascript">
        var max_fields = 8; // maximum input boxes(including the removed) allowed

        function do_generate() {
            var result = "# Dockerfile generated by dockerfileGen\n#\n";

            /* Check version*/
            var version = document.getElementById("version").value;
            if (version != null && version != '') {
                result += "# VERSION " + version + "\n\n";
            }

            /* Check baseimage*/
            var baseimage = document.getElementById("baseimage").value;
            if (baseimage != null && baseimage != '') {
                result += "FROM " + baseimage + "\n";
            }

            /* Check maintainer*/
            var maintainer = document.getElementById("maintainer").value;
            if (maintainer != null && maintainer != '') {
                result += "MAINTAINER " + maintainer + "\n";
            }

            /* Check env*/
            var env = document.getElementById("env").value;
            if (env != null && env != '') {
                result += "ENV " + env + "\n\n";
            }

            /* Check workdir*/
            var workdir = '';
            for (var x = 1; x < max_fields; x++) {
                try {
                    workdir = document.getElementById("workdir" + x).value;
                    if (workdir != null && workdir != '') {
                        result += "WORKDIR " + workdir + "\n";
                    }
                }
                catch (TypeError) {
                    continue;
                }
            }

            /* Check run*/
            var run = document.getElementById("run").value;
            if (run != null && run != '') {
                result += "RUN " + run + "\n\n";
            }

            var expose = '';
            for (var x = 1; x < max_fields; x++) {
                try {
                    expose = document.getElementById("expose_port" + x).value;
                    if (expose != null && expose != '') {
                        result += "EXPOSE " + expose + "\n";
                    }
                }
                catch (TypeError) {
                    continue;
                }
            }

            /* Check add*/
            var add = document.getElementById("add").value;
            if (add != null && add != '') {
                result += "ADD " + add + "\n\n";
            }

            /* Check entrypoint*/
            var entrypoint = document.getElementById("entrypoint").value;
            if (entrypoint != null && entrypoint != '') {
                result += "ENTRYPOINT " + entrypoint + "\n\n";
            }

            /* Check volume*/
            var volume = document.getElementById("volume").value;
            if (volume != null && volume != '') {
                result += "VOLUME " + volume + "\n\n";
            }

            /* Check user*/
            var user = document.getElementById("user").value;
            if (user != null && user != '') {
                result += "USER " + user + "\n\n";
            }

            /* Check manual*/
            var manual = document.getElementById("manual").value;
            if (manual != null && manual != '') {
                result += manual + "\n";
            }


            /* Check cmd*/
            var cmd = document.getElementById("cmd").value;
            if (cmd != null && cmd != '') {
                result += "CMD " + cmd + "\n";
            }

            document.getElementById("result").value = result

            /* Save as file*/
            if (document.getElementById("save").checked) {
                var hiddenElement = document.createElement('a');
                hiddenElement.href = 'data:attachment/text,' + encodeURI(result);
                hiddenElement.target = '_blank';
                hiddenElement.download = 'dockerfile.txt';
                hiddenElement.click();
            }

        }

        $(document).ready(function () {
            var wrapper_expose = $(".input_fields_wrap_expose"); //Fields wrapper
            var add_button_expose = $(".add_field_button_expose"); //Add button ID

            var x = 1; //initial text box count
            $(add_button_expose).click(function (e) { //on add input button click
                e.preventDefault();
                if (x < max_fields) { //max input box allowed
                    x++; //text box increment
                    $(wrapper_expose).append('<div><input type="text" id="expose_port' + x + '" class="form-control col-sm-2"/><a href="#" class="remove_field_expose">Remove</a></div>'); //add input box
                }
            });

            $(wrapper_expose).on("click", ".remove_field_expose", function (e) { //user click on remove text
                e.preventDefault();
                $(this).parent('div').remove();
                //x--;  // TODO: we need more accurate method to tracking the existing ids using x.
            })
        });

        $(document).ready(function () {
            var wrapper_workdir = $(".input_fields_wrap_workdir"); //Fields wrapper
            var add_button_workdir = $(".add_field_button_workdir"); //Add button ID

            var x = 1; //initial text box count
            $(add_button_workdir).click(function (e) { //on add input button click
                e.preventDefault();
                if (x < max_fields) { //max input box allowed
                    x++; //text box increment
                    $(wrapper_workdir).append('<div><input type="text" id="workdir' + x + '" class="form-control col-sm-2"/><a href="#" class="remove_field_workdir">Remove</a></div>'); //add input box
                }
            });

            $(wrapper_workdir).on("click", ".remove_field_workdir", function (e) { //user click on remove text
                e.preventDefault();
                $(this).parent('div').remove();
                //x--;
            })
        });
    </script>

</HEAD>

<body>

<nav class="navbar navbar-inverse">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                    aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">@</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">Dockerfile Generator</a></li>
                <li><a target="_blank" href="https://github.com/yeasy/dockerfileGen">About</a></li>
            </ul>
        </div>
        <!--/.nav-collapse -->
    </div>
</nav>

<div class="container">
    <form id="inputform" class="form-horizontal">

        <div class="form-group">
            <label class="control-label col-sm-2">Version: </label>

            <div class="col-sm-2">
                <input type="text" id="version" class="form-control col-sm-2" value="1.0"/><br>
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-2">Base Image*: </label>

            <div class="col-sm-2">
                <input type="text" id="baseimage" class="form-control col-sm-2" value="ubuntu:14.04" required
                       autofocus/><br>
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-2">Maintainer: </label>
            <div class="col-sm-2">
                <input type="text" id="maintainer" class="form-control col-sm-2" value="maintainer"/>
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-2">USER: </label>
            <div class="col-sm-2">
                <input type="text" id="user" class="form-control" 
                       value="daemon"/><br>
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-2">Expose Port: </label>
            <div class="col-sm-2 input_fields_wrap_expose">
                <input type="text" id="expose_port1" class="form-control col-sm-2" value="80:80"/>
            </div>
            <button type="button" class="btn btn-default btn-success add_field_button_expose">+</button>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-2">ENV: </label>
            <div class="col-sm-4">
                <input type="text" id="env" class="form-control col-sm-2" value="DEBIAN_FRONTEND noninteractive"/><br>
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-2">WORKDIR: </label>
            <div class="col-sm-4 input_fields_wrap_workdir">
                <input type="text" id="workdir1" class="form-control" placeholder="/path/to/workdir"/>
            </div>
            <button type="button" class="btn btn-default btn-success add_field_button_workdir">+</button>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-2">VOLUME: </label>
            <div class="col-sm-4">
                <input type="text" id="volume" class="form-control col-sm-2" 
                       placeholder="['/data']"/><br>
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-2">ADD: </label>
            <div class="col-sm-4">
                <input type="text" id="add" class="form-control col-sm-2" placeholder="./:/data/"/>
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-2">RUN: </label>
            <div class="col-sm-4">
                <input type="text" id="run" class="form-control col-sm-2"
                       placeholder="apt-get update && apt-get install -y supervisor"/><br>
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-2">Manual Ops: </label>

            <div class="col-sm-4">
                <textarea class="form-control" rows="3" cols="80" id="manual"
                          placeholder="Write your specific commands here"></textarea>
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-2">ENTRYPOINT: </label>
            <div class="col-sm-4">
                <input type="text" id="entrypoint" class="form-control col-sm-2" 
                       placeholder="['executable', 'param1', 'param2']"/><br>
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-2">Boot command*: </label>
            <div class="col-sm-4">
                <input type="text" id="cmd" class="form-control" value="['echo', 'hello world']"/><br>
            </div>
        </div>

    </form>

    <div class="col-sm-2">
        <div class="checkbox">
            <label>
                <input type="checkbox" id="save"> Save into file
            </label>
        </div>
        <div class="row">
            <button type="button" class="btn btn-default btn-primary" onclick="do_generate()"> Generate</button>
        </div>
    </div>

    <br>

    <div class="col-sm-6">
        <textarea class="form-control" rows="14" cols="60" id="result" placeholder="generated results"></textarea>
    </div>

</div>
<!-- /container -->

</body>
</html>
